/*
 * MotionTemplate.h
 * MotionTemplate Class
 *
 * author : T. D. Gamage
 */
 
 #pragma once
#include <string>
#include <cv.h>
#include <highgui.h>
//#include "opencv2/core/core.hpp"
//#include <highgui.h>
#include <fstream>
#include "Frame.h"
#include "EarthMoverDistance.h"
#include "SURFeatures.h"
#include "StoppingVehicleDetection.h"
#include <time.h>
using namespace std;
using namespace cv;

// various tracking parameters (in seconds)
const double MHI_DURATION = 1;  //was 1
const double MAX_TIME_DELTA = 0.5; // was 0.5
const double MIN_TIME_DELTA = 0.03; //was 0.05
// number of cyclic frame buffer used for motion detection
// (should, probably, depend on FPS)
//const int N = 4;
const int N = 15; // make it equal to the frame rate


class MotionTemplate
{
private:    
    Frame* currentFrame;
	//Frame* previousSavedFrame; // used to find the stopped object
    IplImage* motionImage;
public:

    IplImage **buf;
    int last;

    // temporary images
    IplImage *mhi; // MHI
    IplImage *orient; // orientation
    IplImage *mask; // valid orientation mask
    IplImage *segmask; // motion segmentation map
    CvMemStorage* storage; // temporary storage
    
    CvPoint prevoiusPoint;
    CvPoint currentPoint;
    CvPoint lastBoxPoint;
	CvPoint lastSavedPoint;
    bool saved;
    char* ipAddress;
    
    
    StoppingVehicleDetection* stoppingVehicleDetection; 
    /*  SV  done * /
	CvRect prevoiusComp_rect;
	CvRect currentComp_rect;
	IplImage *previousRegion;
    IplImage *currentRegion;
	bool check;
	//double oneBeforePreviousOpFlowAve;
	double previousOpFlowAve;
	int vehicleCount;
	ofstream outPutFile;
	/***** SV *****/
    
    

    MotionTemplate(char* ipAddress);
    void setCurrentFrame(Frame* frame);
    void initializeMotionImage(IplImage* image);
    //IplImage* updateTemplate(int diff_threshold );
    IplImage* updateTemplate();
	double drawOptFlowMap(const Mat& flow, Mat& cflowmap, int step,double scale, const Scalar& color);
	void gpuPreprocess(IplImage *buf1,IplImage *buf2,IplImage* image);
	void gpuPostprocess(IplImage* image);
    virtual ~MotionTemplate(void);
    
   int frameCopyKernal(uchar* src);

};